﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using AnguaRPGEngine.Library.Systems;

namespace AnguaRPGEngine.Library.Dice
{
    public class D20 : IDie
    {
        #region ctor
        public D20()
        {
        }
        #endregion
        #region IDie Members
        #region Image
        public System.Drawing.Bitmap Image
        {
            get
            {
                Font frontFont = new Font(FontFamily.GenericSansSerif, 18f);
                Font frontSmallerFont = new Font(FontFamily.GenericSansSerif, 14f);
                Font upperFont = new Font(FontFamily.GenericSansSerif, 8f);
                Font sideFont = new Font(FontFamily.GenericSansSerif, 12f);
                Font bottomFont = new Font(FontFamily.GenericSansSerif, 10f);
                Bitmap i = ImageLibrary.Instance.GetImageFromLibrary(string.Format("Dice.{0}.D{1}", DieRoller.Instance.DiceCollection.CurrentSet.PathName, Sides, _Value));
                using (Graphics g = Graphics.FromImage(i))
                {


                    if (_Value == 6 || _Value == 9)
                    {
                        g.DrawLine(new Pen(new SolidBrush(DieRoller.Instance.DiceCollection.CurrentSet.PipColorAsColor()), 1f), new Point(30, 50), new Point(38, 50));
                    }
                    if (SelectUpperRightSide(_Value) == "6" || SelectUpperRightSide(_Value) == "9")
                    {
                        g.DrawLine(new Pen(new SolidBrush(Color.FromArgb(100, DieRoller.Instance.DiceCollection.CurrentSet.PipColorAsColor())), 1f), new Point(46, 16), new Point(42, 16));
                    }
                    if (SelectUpperLeftSide(_Value) == "6" || SelectUpperLeftSide(_Value) == "9")
                    {
                        g.DrawLine(new Pen(new SolidBrush(Color.FromArgb(40, DieRoller.Instance.DiceCollection.CurrentSet.PipColorAsColor())), 1f), new Point(24, 15), new Point(28, 16));
                    }
                    if (SelectLeftSide(_Value) == "6" || SelectLeftSide(_Value) == "9")
                    {
                        g.DrawLine(new Pen(new SolidBrush(Color.FromArgb(40, DieRoller.Instance.DiceCollection.CurrentSet.PipColorAsColor())), 1f), new Point(19, 36), new Point(22, 32));
                    }
                    if (SelectRightSide(_Value) == "6" || SelectRightSide(_Value) == "9")
                    {
                        g.DrawLine(new Pen(new SolidBrush(Color.FromArgb(150, DieRoller.Instance.DiceCollection.CurrentSet.PipColorAsColor())), 1f), new Point(48, 33), new Point(52, 38));
                    }
                    if (SelectFarRightSide(_Value) == "6" || SelectFarRightSide(_Value) == "9")
                    {
                        g.DrawLine(new Pen(new SolidBrush(Color.FromArgb(60, DieRoller.Instance.DiceCollection.CurrentSet.PipColorAsColor())), 1f), new Point(62, 50), new Point(64, 51));
                    }
                    if (SelectFarLeftSide(_Value) == "6" || SelectFarLeftSide(_Value) == "9")
                    {
                        g.DrawLine(new Pen(new SolidBrush(Color.FromArgb(40, DieRoller.Instance.DiceCollection.CurrentSet.PipColorAsColor())), 1f), new Point(08, 50), new Point(10, 49));
                    }
                    if (SelectSideBelow(_Value) == "6" || SelectSideBelow(_Value) == "9")
                    {
                        g.DrawLine(new Pen(new SolidBrush(Color.FromArgb(100, DieRoller.Instance.DiceCollection.CurrentSet.PipColorAsColor())), 1f), new Point(32, 59), new Point(36, 59));
                    }

                    if (_Value > 9)
                    {
                        g.DrawString(_Value.ToString(), frontSmallerFont, new SolidBrush(DieRoller.Instance.DiceCollection.CurrentSet.PipColorAsColor()), new PointF(20, 32));
                    }
                    else
                    {
                        g.DrawString(_Value.ToString(), frontFont, new SolidBrush(DieRoller.Instance.DiceCollection.CurrentSet.PipColorAsColor()), new PointF(24, 25));
                    }
                    Matrix matrix = new Matrix();
                    matrix.Rotate(-20);
                    g.Transform = matrix;
                    g.DrawString(SelectUpperRightSide(_Value), upperFont, new SolidBrush(Color.FromArgb(100, DieRoller.Instance.DiceCollection.CurrentSet.PipColorAsColor())), new PointF(32, 19));
                    
                    matrix.Reset();
                    g.ResetTransform();

                    matrix.Rotate(44);
                    g.Transform = matrix;
                    g.DrawString(SelectRightSide(_Value), sideFont, new SolidBrush(Color.FromArgb(150, DieRoller.Instance.DiceCollection.CurrentSet.PipColorAsColor())), new PointF(55, -26));

                    matrix.Reset();
                    g.ResetTransform();

                    matrix.Rotate(5);
                    g.Transform = matrix;
                    g.DrawString(SelectFarRightSide(_Value), upperFont, new SolidBrush(Color.FromArgb(60, DieRoller.Instance.DiceCollection.CurrentSet.PipColorAsColor())), new PointF(63, 34));

                    matrix.Reset();
                    g.ResetTransform();

                    matrix.Rotate(26);
                    g.Transform = matrix;
                    g.DrawString(SelectUpperLeftSide(_Value), upperFont, new SolidBrush(Color.FromArgb(40, DieRoller.Instance.DiceCollection.CurrentSet.PipColorAsColor())), new PointF(25, -9));

                    matrix.Reset();
                    g.ResetTransform();

                    matrix.Rotate(-50);
                    g.Transform = matrix;
                    g.DrawString(SelectLeftSide(_Value), sideFont, new SolidBrush(Color.FromArgb(40, DieRoller.Instance.DiceCollection.CurrentSet.PipColorAsColor())), new PointF(-20, 20));

                    matrix.Reset();
                    g.ResetTransform();

                    matrix.Rotate(-13);
                    g.Transform = matrix;
                    g.DrawString(SelectFarLeftSide(_Value), upperFont, new SolidBrush(Color.FromArgb(40, DieRoller.Instance.DiceCollection.CurrentSet.PipColorAsColor())), new PointF(-8, 38));


                    matrix.Reset();
                    g.ResetTransform();

                    matrix.Rotate(-180);
                    g.Transform = matrix;
                    g.DrawString(SelectSideBelow(_Value), bottomFont, new SolidBrush(Color.FromArgb(100, DieRoller.Instance.DiceCollection.CurrentSet.PipColorAsColor())), new PointF(-40, -73));
                }
                return i;
            }
        }
        #endregion
        #region Sides
        public int Sides
        {
            get
            {
                return 20;
            }
        }
        #endregion
        #region Value
        private int _Value = 1;
        public int Value
        {
            get
            {
                return _Value;
            }
        }
        #endregion
        #region Roll
        public void Roll()
        {
            _Value = Randomizer.Instance.NextInt(1, Sides);
        }
        #endregion
        #endregion
        private string SelectUpperRightSide(int value)
        {
            return "";
        }
        
        private string SelectRightSide(int value)
        {
            switch (value)
            {
                case 1:
                    return "3";
                case 2:
                    return "4";
                case 3:
                    return "5";
                case 4:
                    return "6";
                case 5:
                    return "7";
                case 6:
                    return "8";
                case 7:
                    return "9";
                case 8:
                    return "1";
                case 9:
                    return "2";
                case 10:
                    return "3";
                case 11:
                    return "4";
                case 12:
                    return "5";
                case 13:
                    return "6";
                case 14:
                    return "7";
                case 15:
                    return "8";
                case 16:
                    return "9";
                case 17:
                    return "1";
                case 18:
                    return "2";
                case 19:
                    return "3";
            }
            return "4";
        }
        private string SelectFarRightSide(int value)
        {
            return "";
        }
        private string SelectUpperLeftSide(int value)
        {
            return "";
        }
        private string SelectLeftSide(int value)
        {
            switch (value)
            {
                case 1:
                    return "2";
                case 2:
                    return "3";
                case 3:
                    return "4";
                case 4:
                    return "5";
                case 5:
                    return "6";
                case 6:
                    return "7";
                case 7:
                    return "8";
                case 8:
                    return "9";
                case 9:
                    return "1";
                case 10:
                    return "2";
                case 11:
                    return "3";
                case 12:
                    return "4";
                case 13:
                    return "5";
                case 14:
                    return "6";
                case 15:
                    return "7";
                case 16:
                    return "8";
                case 17:
                    return "9";
                case 18:
                    return "1";
                case 19:
                    return "2";
            }
            return "3";
        }
        private string SelectFarLeftSide(int value)
        {
            return "";
        }
        private string SelectSideBelow(int value)
        {
            switch (value)
            {
                case 1:
                    return "5";
                case 2:
                    return "6";
                case 3:
                    return "7";
                case 4:
                    return "8";
                case 5:
                    return "9";
                case 6:
                    return "1";
                case 7:
                    return "2";
                case 8:
                    return "3";
                case 9:
                    return "4";
                case 10:
                    return "5";
                case 11:
                    return "6";
                case 12:
                    return "7";
                case 13:
                    return "8";
                case 14:
                    return "9";
                case 15:
                    return "1";
                case 16:
                    return "2";
                case 17:
                    return "3";
                case 18:
                    return "4";
                case 19:
                    return "5";
            }
            return "6";
        }
    }
}
